﻿@model Sealight.Core.DataAdapter.product
@{
    ViewBag.Title = "Cập nhật sản phẩm";
    Layout = "~/Areas/Admin/Views/Shared/__AdminLayout.cshtml";
}
@section _Head{
    <script type="text/javascript">
        $(function () {
            $("#product_tabs").tabs();
        });
        $(document).ready(function () {
            //<![CDATA[
            // Replace the <textarea id="editor1"> with an CKEditor instance.
            var description_editor;
            if (description_editor)
                description_editor.destroy();
            description_editor = CKEDITOR.replace('description_field', {
                filebrowserImageBrowseUrl: '/file/Index',
                filebrowserImageUploadUrl: '/file/UploadImageWithCKEditor?folder=editors',
                height: 500,
                filebrowserWindowWidth: '640',
                filebrowserWindowHeight: '480'

            });
            // The instanceReady event is fired when an instance of CKEditor has finished
            // its initialization.
            CKEDITOR.on('instanceReady', function (ev) {
                // Show the editor name and description in the browser status bar.

            });
            //]]>
            $('#available_start_date_field').datepicker({
                defaultDate: new Date(),
                dateFormat: 'dd/mm/yy'
            });
            $('#available_end_date_field').datepicker({
                defaultDate: new Date(),
                dateFormat: 'dd/mm/yy'
            });
            var container = $('div.container_error');
            $("#data_form").validate({
                rules: {
                    product_name_field: {// compound rule 
                        required: true
                    }
                },
                messages: {
                    product_name_field: {
                        required: "Vui lòng nhập tên sản phẩm."
                    }
                },
                errorElement: "li",
                errorContainer: container,
                errorLabelContainer: $(".error", container)
            ,
                submitHandler: function (form) {
                    var oEditorDetail = CKEDITOR.instances.description_field;
                    var detail = oEditorDetail.getData();
                    var showHome = false, allowReview = false, allowRating = false, disabledBuy = false;
                    if ($("#show_home_page_field").attr('checked') == 'checked' || $("#show_home_page_field").attr('checked') == 'true')
                        showHome = true;
                    if ($("#allow_customer_review_field").attr('checked') == 'checked' || $("#allow_customer_review_field").attr('checked') == 'true')
                        allowReview = true;
                    if ($("#allow_customer_rating_field").attr('checked') == 'checked' || $("#allow_customer_rating_field").attr('checked') == 'true')
                        allowRating = true;
                    if ($("#disable_buy_button_field").attr('checked') == 'checked' || $("#disable_buy_button_field").attr('checked') == 'true')
                        disabledBuy = true;
                    var data = {
                        id: $("#product_id").val(),
                        name: $("#product_name_field").val(),
                        short_description: $("#short_description_field").val(),
                        full_description: detail,
                        show_on_home_page: showHome,
                        meta_keywords: $("#meta_keywords_field").val(),
                        meta_description: $("#meta_description_field").val(),
                        se_name: $("#seo_title_field").val(),
                        metaTitle: $("#meta_title_field").val(),
                        allow_reviews: allowReview,
                        allow_ratings: allowRating,
                        published: $("#published_field").attr('checked'),
                        type: {
                            id: $('#product_type_field').val()
                        },
                        base_variant: {
                            name: $("#product_name_field").val(),
                            SKU: $("#product_sku_field").val(),
                            description: detail,
                            disable_buy_button: disabledBuy,
                            price: $("#price_field").val(),
                            old_price: $("#old_price_field").val(),
                            product_cost: $("#product_cost_field").val(),
                            weight: $("#weight_field").val(),
                            length: $("#length_field").val(),
                            width: $("#width_field").val(),
                            height: $("#height_field").val(),
                            input_available_start_date: $("#available_start_date_field").val(),
                            input_available_end_date: $("#available_end_date_field").val(),
                            order_minimum_quantity: $('#minimum_cart_quantity_field').val(),
                            order_maximum_quantity: $('#maximum_cart_quantity_field').val()
                        }
                    };

                    JsUtils.ShowLoader('#data_form', 'Lưu...');
                    $.ajax({
                        type: "POST",
                        url: "/admin/product/_SaveProductData",
                        data: JSON.stringify(data),
                        traditional: true,
                        dataType: "json",
                        contentType: 'application/json',
                        error: function (xhr, status, error) { },
                        success: function (d) {
                            if (d != null) {
                                if (d.OperationComplete != null && d.OperationComplete == false && d.ErrorCode != null) {
                                    if (d.ErrorFriendlyMessage != null) {
                                        ShowErrorMessage(d.ErrorFriendlyMessage);
                                    }
                                    return;
                                }
                                else {
                                    ShowErrorMessage("Lưu thành công", "thông báo");
                                    //$('#data_form').get(0).reset();
                                    window.location = "/admin/product/AddUpdateProduct/" + d.id;
                                }
                            }
                        },
                        complete: function (d) {
                            JsUtils.hideLoader('#data_form');
                        },
                        cache: false
                    }); //end .ajax call
                }
            }); //end form.validate   

        });

        $("#nav-left").accordion("activate", 0);
    </script>
}

@if (Model != null)
{
<h2>
    Sửa sản phẩm</h2>
}
else
{
<h2>
    Thêm mới sản phẩm</h2>
}
<div>
    <form id="data_form" method="post" action="" enctype="multipart/form-data">
    <div class="options">
        <input type="submit" value="Lưu" id="btnAddNew" class="adminButtonBlue" title="Lưu"/>
    </div>
    <div class="container_error">
        <h5>
            Vui lòng kiểm tra lại các lỗi...</h5>
        <ol class="error">
        </ol>
    </div>
    <div id="product_tabs">
        <ul>
            <li><a href="#tabs-1">Thông tin sản phẩm</a></li>
            <li><a href="#tabs-2">SEO</a></li>

            @if (Model != null)
            {
             <li><a href="#tabs-8">Sản phẩm (cùng đặc điểm) biến thể</a></li>
            }

            <li><a href="#tabs-3">Thêm Loại cho sản phẩm</a></li>
            <li><a href="#tabs-4">Thêm Nhà sản xuất</a></li>
            <li><a href="#tabs-5">Hình cho sản phẩm</a></li>
            <li><a href="#tabs-6">Sản phẩm liên quan</a></li>
            <li><a href="#tabs-7">Thuộc tính đặc biệt cho sản phẩm</a></li>
        </ul>
        <div id="tabs-1">
            @Html.Partial("_AddUpdateProductInformation", Model)
        </div>
        <div id="tabs-2">
            @Html.Partial("_AddUpdateProductSEO", Model)
        </div>
        @if (Model != null)
        {
         <div id="tabs-8">
            @Html.Partial("_ListProductVariantForProduct", Model)
        </div>
        }
        <div id="tabs-3">
            @if (Model != null)
            {
                @Html.Partial("_CategoryMapForProduct", Model)
            }
            else
            {
                <span>Vui lòng lưu thông tin sản phẩm trước</span>
            }
        </div>
        <div id="tabs-4">
        </div>
        <div id="tabs-5">
            @if (Model != null)
            {
                @Html.Partial("_AddPictureForProduct", Model)
            }
            else
            {
                <span>Vui lòng lưu thông tin sản phẩm trước</span>
            }
        </div>
        <div id="tabs-6">
            @if (Model != null)
            {
                @Html.Action("ListRelateProduct", "Product", new { productId = Model.id })
            }
            else
            {
                <span>Vui lòng lưu thông tin sản phẩm trước</span>
            }
        </div>
        <div id="tabs-7">
            @if (Model != null)
            {
                @Html.Action("ListSpecificationAttributeOptionForProduct", "Product", new { productId = Model.id })
            }
            else
            {
                <span>Vui lòng lưu thông tin sản phẩm trước</span>
            }
        </div>
    </div>
    </form>
</div>
